package kr.ac.kongju.gedl.cmmn.web;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.kongju.gedl.admin.vo.AdminInfoVO;

import org.hsqldb.lib.StringUtil;
import org.springframework.web.filter.OncePerRequestFilter;

public class LoginCheckFilter extends OncePerRequestFilter{

	private String includeUrl;
	
	
	
	public String getIncludeUrl() {
		return includeUrl;
	}



	public void setIncludeUrl(String includeUrl) {
		this.includeUrl = includeUrl;
	}



	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url = request.getRequestURI();
		boolean isContinue = true;
		if(this.includeUrl.contains(url)) {
			Object obj = request.getSession().getAttribute("ADMIN_INFO");
			if(obj instanceof AdminInfoVO) {
				AdminInfoVO vo = (AdminInfoVO)obj;
				if(StringUtil.isEmpty(vo.getAdminId()) || StringUtil.isEmpty(vo.getPassword())) {
					isContinue = false;
				}
			} else {
				isContinue = false;
			}
		}
		
		if(!isContinue) {
			throw new ServletException();
		}
		
		filterChain.doFilter(request, response);
		
	}
	
	

}
